﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using WorkOne.Domain;
using WorkOne.Domain.Model;
using WorkOne.IService;

namespace WorkOne.Webapi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class GoodsController : ControllerBase
    {
        private readonly GoodsDbContext db;
        private readonly IGoodsService segood;

        public GoodsController(IGoodsService segood, GoodsDbContext db)
        {
            this.segood = segood;
            this.db = db;
        }
        /// <summary>
        /// 添加购物车
        /// </summary>
        /// <param name="shopcar"></param>
        /// <returns></returns>
        [HttpGet("AddShopCar")]
        public IActionResult AddShopCar(Shopcar shopcar)
        {
            return Ok(segood.AddShopCar(shopcar));
        }

        /// <summary>
        /// 获取商品首页品牌统计
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetGoodsSticstic")]
        public IActionResult GetGoodsSticstic()
        {
            var list = segood.GetAllGoods().GroupBy(a => a.GoodsType).Select(b => new
            {
                Img = b.ToList()[0].Images,
                Value = b.Key,
                label = b.Count()
            });
            return Ok(list);
        }

        /// <summary>
        /// 获取商品首页轮播图
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet("GetGoodsImages")]
        public IActionResult GetGoodsImages()
        {
            var list = segood.GetAllGoods().OrderBy(a => a.NumSum).Take(4).Select(b => new
            {
                images = b.Images,
            });
            return Ok(list);
        }

        /// <summary>
        /// 获取商品
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet("GetGoods")]
        public IActionResult GetGoods(int pageIndex, int pageSize, string? goodtype = null, decimal maxprice = 0, decimal minprice = 0)
        {
            //var redis = RedisHelpers.Redis;
            int pageCount = 0;
            int totalCount = 0;

            var list = segood.GetAllGoods();

            if (!string.IsNullOrEmpty(goodtype))
            {
                list = list.Where(a => a.GoodsType == goodtype).ToList();
            }
            if (minprice != 0)
            {
                list = list.Where(a => a.Price >= minprice).ToList();
            }
            if (maxprice != 0)
            {
                list = list.Where(a => a.Price <= maxprice).ToList();
            }

            totalCount = list.Count;
            pageCount = (int)Math.Ceiling(totalCount * 1.0 / pageSize);
            list = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return Ok(new
            {
                totalCount = totalCount,
                pageCount = pageCount,
                list = list
            });
        }

        [HttpGet("RedisIs")]
        public IActionResult GetGoods2()
        {
            //var redisclient = RedisHelpers.Redisclient;
            //var redis = redis..Redis;
            //var a = redis.Redis.Get("发未发完");
            return Ok(1);
        }

        [HttpGet("GetGoodsfromSql")]
        public IActionResult GetGoodsfromSql(string name)
        {
            SqlParameter p1 = new SqlParameter("@title", name);
            List<Goods> goods = db.Goods.FromSqlRaw("exec GetGoodsById @title", p1).ToList();
            return Ok(goods);
        }

        //var redisclient = RedisHelpers.Redisclient;
        //var redis = RedisHelpers.Redis;
        /// <summary>
        /// 添加商品
        /// </summary>
        /// <param name="goods"></param>
        /// <returns></returns>
        [HttpPost("PostGoods")]
        public IActionResult PostGoods(Goods goods)
        {
            return Ok(segood.GoodsAdd(goods));
        }

        [HttpPost("SqlUpdate")]
        public IActionResult SqlUpdate(Goods g)
        {
            SqlParameter[] p1 = new SqlParameter[]{
         new SqlParameter("@Id", g.Id),
         new SqlParameter("@Name", g.Name),
         new SqlParameter("@Desc", g.Desc),
         new SqlParameter("@Images", g.Images),
         new SqlParameter("@State", g.State),
         new SqlParameter("@GoodsType", g.GoodsType),
         new SqlParameter("@Price", g.Price),
         new SqlParameter("@IsDel", g.IsDel),
         new SqlParameter("@CreatePeople", g.CreatePeople),
         new SqlParameter("@CreateTime", g.CreateTime),
         new SqlParameter("@UpdatePeople", g.UpdatePeople),
         new SqlParameter("@UpdateTime", g.UpdateTime),
         new SqlParameter("@NumSum", g.NumSum),
                           };
            var a = db.Database.ExecuteSqlRaw("exec UpdateGoods @Id,@Name" +
                 ",@Desc,@Images,@State,@GoodsType,@Price,@IsDel,@CreatePeople,@CreateTime,@UpdatePeople,@UpdateTime,@NumSum", p1);
            return Ok(a);
        }
    }
}